﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Web.UI.HtmlControls;

    using MarketPulse.BusinessLogic.Legacy;

#endregion

namespace MarketPulse.Web.Helpers.Legacy
{
    public static class AccreditationControlHelper
    {

        #region Constants

            /// <summary>Constant value for No</summary>
            private const string NO = "NO";

            /// <summary>Constant value for Yes</summary>
            private const string YES = "YES";

        #endregion

        #region Methods

            /// <summary>
            /// Method used to create the control necessary to render the Display Question section of the current page
            /// </summary>
            /// <param name="page">The page of the accreditation this answer is part of</param>
            /// <param name="accreditationTitle">The title of the accreditation this answer is part of</param>           
            /// <returns>A valid (filled) Html Generic control (for the Display Question)</returns>
            public static HtmlGenericControl ConstructQuestionSectionHorizontal(AccreditationPage page, string accreditationTitle)
            {
                // Setup the Question section
                HtmlGenericControl questionDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.ID, "displayQuestion" } });
                HtmlGenericControl questionDetailDiv = GetDisplayQuestionDetailControls(page.QuestionText, page.InstructionText);
                questionDiv.Controls.Add(questionDetailDiv);
                // Setup the Answers section
                HtmlGenericControl answersDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.ID, "displayAnswers" } });
                // Correct answer section
                HtmlGenericControl answerDiv = GetAnswerControls(page, accreditationTitle, "YES", "correctAnswerHeader", "correctAnswerImage");
                answersDiv.Controls.Add(answerDiv);
                questionDiv.Controls.Add(answersDiv);
                // Clear any floating
                HtmlGenericControl clearedDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.CLASS, "clear" } });
                answersDiv.Controls.Add(clearedDiv);
                // Incorrect answer section
                answerDiv = GetAnswerControls(page, accreditationTitle, "NO", "incorrectAnswerHeader", "incorrectAnswerImage");
                answersDiv.Controls.Add(answerDiv);
                questionDiv.Controls.Add(answersDiv);

                return questionDiv;
            }

            /// <summary>
            /// Method used to create the control necessary to render the Display Question section of the current page
            /// </summary>
            /// <param name="page">The page of the accreditation this answer is part of</param>
            /// <param name="accreditationTitle">The title of the accreditation this answer is part of</param>
            /// <returns>A valid (filled) Html Generic control (for the Display Question)</returns>
            public static HtmlGenericControl ConstructQuestionSectionVertical(AccreditationPage page, string accreditationTitle)
            {
                HtmlGenericControl questionDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.ID, "displayQuestion" } });
                HtmlGenericControl questionDetailDiv = GetDisplayQuestionDetailControls(page.QuestionText, page.InstructionText);

                questionDiv.Controls.Add(questionDetailDiv);

                // Setup the Question section
                var containerDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "verticalQuestionAnswer" } });

                foreach (var DisplayAnswer in page.DisplayQuestion.DisplayAnswers)
                {
                    var itemDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "questionAnswerItemContainer" } });
                    var itemTitle = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "questionAnswerItemTitle" } });

                    string imageLabel = string.IsNullOrEmpty(DisplayAnswer.Title) ? DisplayAnswer.AnswerLabel : DisplayAnswer.Title;
                    itemTitle.Style.Add("background", String.Format("{0} url(Accreditations/{1}/Images/{2}.gif) no-repeat left bottom", DisplayAnswer.AnswerBackColor, accreditationTitle, imageLabel));
                    if (!string.IsNullOrEmpty(DisplayAnswer.Height))
                    {
                        itemTitle.Style.Add("height", DisplayAnswer.Height + "px");
                    }
                    itemDiv.Controls.Add(itemTitle);

                    var itemContent = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "questionAnswerItemContent" } });
                    itemContent.InnerHtml = DisplayAnswer.AnswerText;
                    if (!String.IsNullOrEmpty(DisplayAnswer.AnswerTextCssClass))
                    {
                        itemContent.Attributes["class"] = String.Concat(itemContent.Attributes["class"], " ", DisplayAnswer.AnswerTextCssClass);

                        if (!string.IsNullOrEmpty(DisplayAnswer.Height))
                        {
                            itemContent.Style.Add("height", Convert.ToString(int.Parse(DisplayAnswer.Height) + 10) + "px");
                        }
                    }
                    itemDiv.Controls.Add(itemContent);

                    if (!string.IsNullOrEmpty(DisplayAnswer.AnswerImage))
                    {
                        var itemImageSectionDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.ID, "itemImageSectionDiv" } });

                        var itemImageContainer = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "questionAnswerItemImage" } });
                        itemImageContainer.Style.Add("background-color", DisplayAnswer.AnswerBackColor);
                        Dictionary<string, string> attributes = new Dictionary<string, string>();
                        attributes.Add(HtmlControlHelper.ALT, DisplayAnswer.AnswerImageText);
                        attributes.Add(HtmlControlHelper.SRC, "Accreditations/" + accreditationTitle + "/Images/Answers/" + DisplayAnswer.AnswerImage);
                        var itemImage = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.IMAGE, attributes);
                        itemImageContainer.Controls.Add(itemImage);
                        itemImageSectionDiv.Controls.Add(itemImageContainer);

                        if (!string.IsNullOrEmpty(DisplayAnswer.ShowImageTips) && "Y".Equals(DisplayAnswer.ShowImageTips))
                        {
                            var itemImageTipContainer = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.ID, "questionAnswerItemImageTip" } });
                            itemImageTipContainer.Style.Add("float", "left");
                            itemImageTipContainer.Style.Add("margin-left", "10px");
                            itemImageTipContainer.Style.Add("margin-top", "20px");
                            itemImageTipContainer.Style.Add("width", "150px");

                            var itemTipContainer = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.ID, "questionAnswerItemTip" } });
                            itemTipContainer.Style.Add("height", "40px");
                            itemTipContainer.Style.Add("margin-bottom", "10px");

                            var itemTipText = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.H2, new Dictionary<string, string> { { HtmlControlHelper.ID, "questionAnswerItemTipText" } });
                            itemTipText.InnerText = DisplayAnswer.AnswerImageText;
                            itemTipText.Style.Add("color", "grey");
                            itemTipText.Style.Add("font-size", "14px");
                            itemTipText.Style.Add("text-align", "left");
                            itemTipContainer.Controls.Add(itemTipText);
                            itemImageTipContainer.Controls.Add(itemTipContainer);

                            attributes = new Dictionary<string, string>();
                            attributes.Add(HtmlControlHelper.SRC, "Accreditations/" + accreditationTitle + "/Images/" + DisplayAnswer.AnswerLabel + "BigAnswer.jpg");
                            attributes.Add("width", "50px");
                            HtmlGenericControl imageTip = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.IMAGE, attributes);
                            itemImageTipContainer.Controls.Add(imageTip);

                            itemImageSectionDiv.Controls.Add(itemImageTipContainer);
                        }
                        itemDiv.Controls.Add(itemImageSectionDiv);
                    }
                    containerDiv.Controls.Add(itemDiv);
                }

                questionDiv.Controls.Add(containerDiv);

                return questionDiv;
            }

            /// <summary>
            /// Method used to create the control necessary to render the Display Question section of the current page
            /// </summary>
            /// <param name="page">The page of the accreditation this answer is part of</param>
            /// <param name="accreditationTitle">The title of the accreditation this answer is part of</param>            
            /// <returns>A valid (filled) Html Generic control (for the Display Question)</returns>
            public static HtmlGenericControl ConstructQuestionSectionHorizontal3Row(AccreditationPage page, string accreditationTitle)
            {
                HtmlGenericControl questionDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.ID, "displayQuestion" } });
                HtmlGenericControl questionDetailDiv = GetDisplayQuestionDetailControls(page.QuestionText, page.InstructionText);

                questionDiv.Controls.Add(questionDetailDiv);

                // Setup the Question section
                var containerDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "horizontal3RowQuestionAnswer" } });
                var displayQuestionDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.ID, "question" } }); ;
                int index = 0;
                foreach (var DisplayAnswer in page.DisplayQuestion.DisplayAnswers)
                {
                    int result = index % 2;
                    if (result == 0)
                    {
                        displayQuestionDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.ID, "question" } });
                        displayQuestionDiv.Style.Add("border", "1px");
                        var questionTitleDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.ID, "answerTitle" } });
                        questionTitleDiv.InnerHtml = DisplayAnswer.Title;
                        displayQuestionDiv.Controls.Add(questionTitleDiv);
                    }

                    var itemDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "horizontal3RowQuestionAnswerItemContainer" } });
                    var itemTitle = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "horizontal3RowQuestionAnswerItemTitle" } });
                    itemTitle.Style.Add("background", String.Format("{0} url(Accreditations/{1}/Images/{2}.gif) no-repeat left bottom", DisplayAnswer.AnswerBackColor, accreditationTitle, DisplayAnswer.AnswerLabel));
                    itemDiv.Controls.Add(itemTitle);
                    var itemContent = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "horizontal3RowQuestionAnswerItemContent" } });
                    itemContent.InnerHtml = DisplayAnswer.AnswerText;
                    if (!String.IsNullOrEmpty(DisplayAnswer.AnswerTextCssClass))
                        itemContent.Attributes["class"] = String.Concat(itemContent.Attributes["class"], " ", DisplayAnswer.AnswerTextCssClass);
                    itemDiv.Controls.Add(itemContent);
                    var itemImageContainer = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string> { { HtmlControlHelper.CLASS, "horizontal3RowQuestionAnswerItemImage" } });
                    itemImageContainer.Style.Add("background-color", DisplayAnswer.AnswerBackColor);
                    Dictionary<string, string> attributes = new Dictionary<string, string>();
                    attributes.Add(HtmlControlHelper.ALT, DisplayAnswer.AnswerImageText);
                    displayQuestionDiv.Controls.Add(itemDiv);
                    containerDiv.Controls.Add(displayQuestionDiv);

                    index++;
                }
                questionDiv.Controls.Add(containerDiv);

                return questionDiv;
            }

            #region Local Methods
        
                /// <summary>
                /// Method used to build the control collection used to represent the DisplayQuestion
                /// </summary>
                /// <param name="questionText">The question text for the question to display</param>
                /// <param name="instructionText">The instruction text for the question to display</param>
                /// <returns>A valid (filled) html control</returns>
                private static HtmlGenericControl GetDisplayQuestionDetailControls(string questionText, string instructionText)
                {
                    // Instantiate the return object
                    HtmlGenericControl temporaryQuestionDetailDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.ID, "displayQuestionDetail" } });

                    // Create Question Controls
                    HtmlGenericControl questionTable = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.TABLE, new Dictionary<string, string>());
                    questionTable.Attributes.Add("cellpadding", "0");
                    questionTable.Attributes.Add("cellspacing", "0");
                    HtmlGenericControl questionTableTr = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.TR, new Dictionary<string, string>());
                    HtmlGenericControl questionBulletTableTd = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.TD, new Dictionary<string, string>() { { HtmlControlHelper.CLASS, "questionBulletTD" } });
                    HtmlGenericControl questionTableTd = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.TD, new Dictionary<string, string>());

                    // TODO Add 'font' control type to the constants in Html helper
                    HtmlGenericControl questionBulletFont = new HtmlGenericControl("font");
                    questionBulletFont.Attributes.Add("class", "questionBullet");
                    questionBulletFont.InnerText = "Q.";

                    questionBulletTableTd.Controls.Add(questionBulletFont);
                    questionTableTr.Controls.Add(questionBulletTableTd);

                    HtmlGenericControl questionTextP = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.ID, "displayQuestion-text" } });
                    questionTextP.InnerHtml = questionText;
                    questionTableTd.Controls.Add(questionTextP);

                    HtmlGenericControl questionInstructionP = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.CLASS, "displayQuestion-instruction" } });
                    questionInstructionP.InnerHtml = instructionText;

                    questionTableTd.Controls.Add(questionInstructionP);

                    questionTableTr.Controls.Add(questionTableTd);
                    questionTable.Controls.Add(questionTableTr);

                    temporaryQuestionDetailDiv.Controls.Add(questionTable);

                    return temporaryQuestionDetailDiv;
                }

                /// <summary>
                /// Method used to retrieve the controls used to represent the answer section of a DisplayQuestion
                /// </summary>
                /// <param name="page">The page of the accreditation this answer is part of</param>
                /// <param name="accreditationTitle">The title of the accreditation this answer is part of</param>
                /// <param name="labelText">The text to dispay as the label for this answer</param>
                /// <param name="headerStyleClassName">The style class to use for the header for this answer</param>
                /// <param name="imageStyleClassName">The style class to use for the image for this answer</param>
                /// <returns>A valid (filled) html control</returns>
                private static HtmlGenericControl GetAnswerControls(AccreditationPage page,  string accreditationTitle, string labelText, string headerStyleClassName, string imageStyleClassName)
                {
                    // Instantiate return object
                    HtmlGenericControl temporaryAnswerDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.CLASS, "displayAnswer" } });

                    HtmlGenericControl header = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.H2, new Dictionary<string, string>() { { HtmlControlHelper.CLASS, headerStyleClassName } });
                    header.InnerText = labelText;

                    HtmlGenericControl imageDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.CLASS, imageStyleClassName } });

                    HtmlGenericControl yesTable = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.TABLE, new Dictionary<string, string>());
                    HtmlGenericControl yesTableTr = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.TR, new Dictionary<string, string>());
                    HtmlGenericControl yesTableTd = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.TD, new Dictionary<string, string>());

                    HtmlGenericControl listDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.CLASS, "displayAnswerList" } });

                    HtmlGenericControl answerTextP = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.ID, "displayAnswer-text" } });

                    Dictionary<string, string> attributes = new Dictionary<string, string>();

                    switch (labelText)
                    {
                        case NO:
                            answerTextP.InnerHtml = page.DisplayQuestion.DisplayAnswers[1].AnswerText;
                            attributes.Add(HtmlControlHelper.ALT, page.DisplayQuestion.DisplayAnswers[1].AnswerImageText);
                            attributes.Add(HtmlControlHelper.SRC, "Accreditations/" + accreditationTitle + "/Images/Answers/" + page.DisplayQuestion.DisplayAnswers[1].AnswerImage);
                            break;
                        case YES:
                            answerTextP.InnerHtml = page.DisplayQuestion.DisplayAnswers[0].AnswerText;
                            attributes.Add(HtmlControlHelper.ALT, page.DisplayQuestion.DisplayAnswers[0].AnswerImageText);
                            attributes.Add(HtmlControlHelper.SRC, "Accreditations/" + accreditationTitle + "/Images/Answers/" + page.DisplayQuestion.DisplayAnswers[0].AnswerImage);
                            break;
                        default:
                            // Do nothing
                            break;
                    }

                    HtmlGenericControl image = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.IMAGE, attributes);

                    listDiv.Controls.Add(answerTextP);

                    yesTableTd.Controls.Add(listDiv);
                    yesTableTr.Controls.Add(yesTableTd);
                    yesTable.Controls.Add(yesTableTr);

                    temporaryAnswerDiv.Controls.Add(header);

                    imageDiv.Controls.Add(image);
                    temporaryAnswerDiv.Controls.Add(imageDiv);

                    temporaryAnswerDiv.Controls.Add(yesTable);

                    // Clear any floating
                    HtmlGenericControl clearedDiv = HtmlControlHelper.AddGenericHtmlControl(HtmlControlHelper.DIV, new Dictionary<string, string>() { { HtmlControlHelper.CLASS, "clear" } });
                    temporaryAnswerDiv.Controls.Add(clearedDiv);

                    return temporaryAnswerDiv;
                }

            #endregion

        #endregion

    }
}
